document.addEventListener('DOMContentLoaded', function() {
    // 获取元素
    const modal = document.getElementById('customModal');
    const openBtn = document.getElementById('openModal');
    const closeBtn = document.querySelector('.close-btn');
    const secondaryBtn = document.querySelector('.btn.secondary');
    
    // 打开弹窗
    openBtn.addEventListener('click', function() {
        modal.classList.add('show');
        document.body.style.overflow = 'hidden'; // 防止背景滚动
    });
    
    // 关闭弹窗的函数
    function closeModal() {
        modal.classList.remove('show');
        document.body.style.overflow = ''; // 恢复背景滚动
        
        // 监听过渡结束后完全隐藏
        modal.addEventListener('transitionend', function handler() {
            modal.style.display = 'none';
            modal.removeEventListener('transitionend', handler);
        });
    }
    
    // 点击关闭按钮
    closeBtn.addEventListener('click', closeModal);
    
    // 点击取消按钮
    secondaryBtn.addEventListener('click', closeModal);
    
    // 点击弹窗外部关闭
    modal.addEventListener('click', function(event) {
        if (event.target === modal) {
            closeModal();
        }
    });
    
    // ESC键关闭弹窗
    document.addEventListener('keydown', function(event) {
        if (event.key === 'Escape' && modal.classList.contains('show')) {
            closeModal();
        }
    });
});